
package com.longrise.android.byjk.app;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.longrise.android.Global;
import com.longrise.android.byjk.BuildConfig;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.datasource.remote.ReloginUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.PrintLog;
import com.longrise.common.baseapp.BaseAppLike;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;


/**
 * Created by callxkj on 2017/3/22.
 * applicationlike类
 */

public class BYJKAppLike extends BaseAppLike {

    private static final String TAG = "BaseAppLike";
    private static Application mAPP;
    public static IWXAPI mIWXAPI;

    private static final String PROPERTY_ID = "UA-70302465-4";

    public BYJKAppLike(Application application, int tinkerFlags,
                       boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
                       long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag,
                applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mAPP = getApplication();
        initLoadManager();
        initPrintLog();
        registerWXAPI();
        initImageLoader();
        registerBroadCast();
        initVersion();
        initBugly();
    }

    /**
     * 获取application 对象
     */
    public static Application getAPP() {
        return mAPP;
    }

    /**
     * 打印日子的工具类 初始化
     */
    private void initPrintLog() {
        if (BuildConfig.DEBUG) {
            PrintLog.setStatus(PrintLog.LEVEL_ERROR);
        } else {
            PrintLog.setStatus(PrintLog.LEVEL_NONE);
        }
    }

    private void registerWXAPI() {
        mIWXAPI = WXAPIFactory.createWXAPI(mAPP.getApplicationContext(), UserInfor.getInstance().APP_ID, true);
        boolean registerApp = mIWXAPI.registerApp(UserInfor.getInstance().APP_ID);
        PrintLog.d(TAG, registerApp + "");
    }


    private void initImageLoader() {
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(mAPP.getApplicationContext());

        //Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);
    }


    /**
     * 网络异常的广播 loaddatamanager里面发出
     */
    private void registerBroadCast() {
        BBnetworkBroadCast broadCast = new BBnetworkBroadCast();
        IntentFilter filter = new IntentFilter();
        filter.addAction("nonetwork");
        mAPP.registerReceiver(broadCast, filter);
    }


    /**
     * 无网络广播
     */
    class BBnetworkBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            DZZWTools.showToast(mAPP.getApplicationContext(), mAPP.getString(R.string.nonetwork), 2 * 1000);
        }
    }

    /**
     * 新的网络加载类的初始化
     */
    private void initLoadManager(){
        int appVersionCode = AppUtil.getAppVersionCode();
        LoadDataManager.getInstance().init(String.valueOf(appVersionCode));
        //设置Session超时再次登录成功实现类的完整路径

        LoadDataManager.getInstance().setReloginPath("com.longrise.common.datasource.remote.ReloginUtil");
    }


    private void initVersion() {
        try {
            PackageManager pm = mAPP.getPackageManager();
            if (null != pm) {
                PackageInfo packageInfo = pm.getPackageInfo(mAPP.getPackageName(), 0);
                if (null != packageInfo) {
                    Global.getInstance().setVersionCode(packageInfo.versionCode);
                    Global.getInstance().setVersionName(packageInfo.versionName);
                    CacheUtils.setString("AppVersion", packageInfo.versionCode + "");
                }
                packageInfo = null;
            }
        } catch (Exception e) {

        }

    }


    /**
     * 初始化bugly
     * 自动在release版本配置bugly
     */
    private void initBugly() {
        if (BuildConfig.DEBUG) {
            // 设置开发设备，默认为false，上传补丁如果下发范围指定为“开发设备”，需要调用此接口来标识开发设备
            Bugly.setIsDevelopmentDevice(getApplication(), true);
            Bugly.init(getApplication(), "026d73e8f2", true);
        } else {
            // 设置开发设备，默认为false，上传补丁如果下发范围指定为“开发设备”，需要调用此接口来标识开发设备
            Bugly.setIsDevelopmentDevice(getApplication(), false);
            CrashReport.initCrashReport(mAPP.getApplicationContext(), "026d73e8f2", false);
            Bugly.init(getApplication(), "026d73e8f2", false);
        }
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);

        // 安装tinker
        // TinkerManager.installTinker(this); 替换成下面Bugly提供的方法
        Beta.installTinker(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }


}
